using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JlPay.Sdk.Models.Trans.Dto
{
    /// <summary>
    /// 商品数据DTO
    /// @author liaochangyi
    /// @since 2025-01-25
    /// </summary>
    public class GoodsDataDto
    {
        /// <summary>
        /// 订单原价
        /// </summary>
        [JsonPropertyName("cost_price")]
        public string? CostPrice { get; set; }

        /// <summary>
        /// 商品小票ID
        /// </summary>
        [JsonPropertyName("receipt_id")]
        public string? ReceiptId { get; set; }

        /// <summary>
        /// 单品列表
        /// </summary>
        [JsonPropertyName("goods_detail")]
        public List<GoodsDetailDto>? GoodsDetail { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public GoodsDataDto()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="goodsDataDict">商品数据字典</param>
        public GoodsDataDto(Dictionary<string, object>? goodsDataDict)
        {
            if (goodsDataDict != null)
            {
                CostPrice = goodsDataDict.TryGetValue("cost_price", out var costPrice) ? costPrice?.ToString() : null;
                ReceiptId = goodsDataDict.TryGetValue("receipt_id", out var receiptId) ? receiptId?.ToString() : null;

                if (goodsDataDict.TryGetValue("goods_detail", out var goodsDetailData) && goodsDetailData != null)
                {
                    if (goodsDetailData is List<object> detailList)
                    {
                        GoodsDetail = new List<GoodsDetailDto>();
                        foreach (var detail in detailList)
                        {
                            if (detail is Dictionary<string, object> detailDict)
                            {
                                GoodsDetail.Add(new GoodsDetailDto(detailDict));
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 设置商品详情列表
        /// </summary>
        /// <param name="goodsDetail">商品详情列表，可以是字典数组或GoodsDetailDto对象数组</param>
        /// <returns>当前对象</returns>
        public GoodsDataDto SetGoodsDetail(List<object>? goodsDetail)
        {
            if (goodsDetail != null)
            {
                GoodsDetail = new List<GoodsDetailDto>();
                foreach (var detail in goodsDetail)
                {
                    if (detail is Dictionary<string, object> detailDict)
                    {
                        GoodsDetail.Add(new GoodsDetailDto(detailDict));
                    }
                    else if (detail is GoodsDetailDto goodsDetailDto)
                    {
                        GoodsDetail.Add(goodsDetailDto);
                    }
                    else
                    {
                        // 如果不是字典也不是GoodsDetailDto对象，尝试转换为GoodsDetailDto
                        GoodsDetail.Add(new GoodsDetailDto());
                    }
                }
            }
            else
            {
                GoodsDetail = null;
            }
            return this;
        }

        /// <summary>
        /// 转换为字典格式
        /// </summary>
        /// <returns>字典数据</returns>
        public Dictionary<string, object?> ToDictionary()
        {
            var goodsDetailArray = new List<Dictionary<string, object?>>();
            if (GoodsDetail != null)
            {
                foreach (var detail in GoodsDetail)
                {
                    goodsDetailArray.Add(detail.ToDictionary());
                }
            }

            return new Dictionary<string, object?>
            {
                ["cost_price"] = CostPrice,
                ["receipt_id"] = ReceiptId,
                ["goods_detail"] = goodsDetailArray
            };
        }
    }
} 